package com.wonder.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.SfcOperp;
import com.wonder.app.domain.SyncOrder;
import com.wonder.app.domain.SyncOrderDetail;
import com.wonder.app.mapper.SyncOrderDao;
import com.wonder.app.mapper.SyncOrderDetailDao;
import com.wonder.app.request.SyncOrderRequest;
import com.wonder.app.service.SyncOrderDetailService;
import com.wonder.app.service.SyncOrderService;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.utils.DateUtils;
import com.wonder.common.utils.StringUtils;
import com.wonder.constant.BillType;
import com.wonder.constant.IXqcConst;
import jnr.constants.platform.PRIO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SyncOrderServiceImpl extends ServiceImpl<SyncOrderDao, SyncOrder> implements SyncOrderService {
    @Autowired
    private SyncOrderDetailService syncOrderDetailService;

    @Autowired
    private SyncOrderDetailDao syncOrderDetailDao;


    @Autowired
    private SyncOrderDao syncOrderDao;

    @Override
    public IPage<SyncOrder> selectList(SyncOrderRequest request, Page<SyncOrder> page) {
        LambdaQueryWrapper<SyncOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!VtuzxUtil.isEmpty(request.getBillNo())){
            lambdaQueryWrapper.like(SyncOrder::getBillNo,request.getBillNo());
        }

        if (!VtuzxUtil.isEmpty(request.getUserName())){
            lambdaQueryWrapper.like(SyncOrder::getUserName,request.getUserName());
        }
        if (request.getStartTime() != null){
            Date[] orderDate = request.getStartTime();
            if(orderDate.length>0)
                lambdaQueryWrapper.ge(SyncOrder::getStartTime, orderDate[0]);
            if(orderDate.length==2)
                lambdaQueryWrapper.le(SyncOrder::getStartTime, DateUtils.dateToStr(orderDate[1],"yyyy-MM-dd")+ " 23:59:59");

        }
        lambdaQueryWrapper.orderByDesc(SyncOrder::getId);
        Page<SyncOrder> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return selectPage;
    }

    @Override
    public String startSyncOrder(String name, Long userId, String type) {

        String billNo = IXqcConst.createBillNo(BillType.DDTB);
        SyncOrder syncOrder = new SyncOrder();
        syncOrder.setStartTime(new Date());
        syncOrder.setBillNo(billNo);
        syncOrder.setType(type);
//        syncOrder.setUserId();
        syncOrder.setUserName(name);
        syncOrderDao.insert(syncOrder);
        return billNo;
    }

    @Override
    public Map<String,Object> endSyncOrder(List<SyncOrderDetail> orderDetailList, String tbbillNo) {
        syncOrderDetailService.saveOrUpdateBatch(orderDetailList);
        int successCount = 0;
        int failCount = 0;
        for (SyncOrderDetail syncOrderDetail : orderDetailList){
            Boolean isSuccess = syncOrderDetail.getIsSuccess();
            if (isSuccess){
                successCount ++;
            } else {
                failCount ++;
            }
        }
        SyncOrder syncOrder = syncOrderDao.selectOne(new LambdaQueryWrapper<SyncOrder>()
                .eq(SyncOrder::getBillNo,tbbillNo));
        syncOrder.setCount(orderDetailList.size());
        syncOrder.setFailCount(failCount);
        syncOrder.setSuccessCount(successCount);
        syncOrder.setEndTime(new Date());
        syncOrderDao.updateById(syncOrder);
        Map<String,Object> data = new HashMap<>();
        data.put("successCount",successCount);
        data.put("failCount",failCount);
        return data;
    }

    @Override
    public Map<String, Object> dumpdata(Map<String, Object> map) {
        String billNo = map.get("billNo").toString();
        List<SyncOrderDetail> orderDetailList = syncOrderDetailDao
                .selectList(new LambdaQueryWrapper<SyncOrderDetail>().eq(SyncOrderDetail::getBillNo, billNo));
        List<Map<String,Object>> dataList = new ArrayList<>();
        //整理一下需要的数据
        for (SyncOrderDetail syncOrderDetail : orderDetailList){
            Boolean isSuccess = syncOrderDetail.getIsSuccess();
            String orderNo = syncOrderDetail.getOrderNo();
            String message = syncOrderDetail.getMessage();

            Map<String,Object> orderMap = new LinkedHashMap<>();
            orderMap.put("orderNo",orderNo);

            if (isSuccess){
                orderMap.put("isSuccess","成功");
            } else {
                orderMap.put("isSuccess","失败");
            }
            orderMap.put("message",message);
            dataList.add(orderMap);
        }
        String header = "订单编号,是否成功,错误信息";
        Map<String,Object> retMap = new HashMap<>();
        retMap.put("header",StringUtils.split(header,','));
        retMap.put("filename", "订单同步.xlsx");
        retMap.put("percentage", dataList.size());
        retMap.put("data", dataList);
        return AjaxMap.success(retMap);
    }
}
